<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>智慧园区-登录</h1>

      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username" label="账号">
            <el-input v-model="loginForm.username" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item prop="password" label="密码">
            <el-input v-model="loginForm.password" placeholder="请输入密码" type="password" show-password />
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="selected">记住我</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button style="width:350px" type="primary" @click="toLogin">登录</el-button>
            <el-button @click="getuser">打印账号密码</el-button>
          </el-form-item>
        </el-form>
      </el-card>

    </div>
  </div>
</template>

<script>
import { getuserInfo, removeuserInfo, setuserInfo } from '@/utils/user'

export default {
  name: 'Login',
  data() {
    return {
      selected: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    const res = getuserInfo()
    if (res) {
      console.log(1)
      this.loginForm.password = JSON.parse(res).password
      this.loginForm.username = JSON.parse(res).username
    }
  },
  methods: {
    async toLogin() {
      this.$refs.loginRef.validate(async e => {
        if (e) {
          await this.$store.dispatch('user/login', this.loginForm)
          if (this.selected) {
            removeuserInfo()
            setuserInfo(this.loginForm)
          }
        }
      })
    },
    getuser() {
      console.log(this.loginForm)
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
    display: flex;
    align-items: stretch;
    height: 100vh;
    .logo {
      width: 60vw;
      background: rgb(26, 76, 225) url(../../assets/common/login-bg-e4ea539c.svg)
        no-repeat center / cover;
      border-top-right-radius: 60px;
    }
    .form {
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding-left: 176px;
      .el-card {
        border: none;
        padding: 0;
      }
      h1 {
        padding-left: 20px;
        font-weight: 500;
        font-size: 30px;
      }
      ::v-deep .el-input {
        width: 350px;
        height: 44px;
        .el-input__inner {
          background: #f4f5fb;
        }
      }
      ::v-deep .el-checkbox {
        color:#606266;
      }
    }
    }
</style>
